/*Do remove or display obj when the obj is active but click other obj*/
var EAOutDown = function(e){
    if(GH == null) return;
    if(isObj(GH))
    {
        if(isChild(GH,__E(e))) {return;}
        try{
        GH.parentNode.removeChild(GH);}catch(e){}
        GH = null;
        return;
    }
    var func = __call('EA_down_'+GH);
    if(func) func(e)
}
/*mouse down the user log text what event action*/
var EA_down_userLog = function(e)
{
    var eObj = __E(e);
    if(GH != 'userLog') EAOutDown(e);
    if(Emt.isId('userLogInput') == false)
    {
        var text = eObj.innerHTML;
        eObj.innerHTML = '';
        var inputObj = __createNode(eObj,'input','userLogInput');
        __setAtt(inputObj,'value',text);
        __setAtt(inputObj,'type', 'text');
        __setAtt(inputObj,'size','100');
        EA('userLog',true);
    }else if(eObj.id == "userLog" || eObj.id == "userLogInput"){
        EA('userLog',true);
        return;
    }else{
        var text = $('userLogInput').value;
        $('userLog').innerHTML = text;
        EA(null,false);
    }
}
/*event action what dispaly user msg list when the mouse down msg btn in user panel  */
var EA_down_msgBox_Btn = function(e)
{
    var eObj = __E(e);
    if(GH != 'msgBox_Btn') EAOutDown(e);
    if(Emt.isId('msgBox') == false)
    {
        createMenu(eObj,'msgBox',msgBox,'menuitem','忽略');
        EA('msgBox_Btn');
    }else{
        if($('msgBox').style.display == 'none')
        {
            $('msgBox').style.display = 'block';
            EA('msgBox_Btn',true);
        }else{
            $('msgBox').style.display = "none";
            EA(null,false);
        }
    }
    return;
}
/*event action what dispaly user menu when mouse down user head show*/
var EA_down_showImg = function(e)
{
    var eObj = __E(e);
    if(GH != 'showImg') EAOutDown(e);
    if(Emt.isId('mainMenu') == false)
    {
        createMenu(eObj,'mainMenu',mainMenu,'menuitem');
        EA('showImg',true); 
    }else{
        if($('mainMenu').style.display == 'none')
        {
            $('mainMenu').style.display = 'block';
            EA('showImg',true);
        }else{
            $('mainMenu').style.display = "none";
            EA(null,false);
        }
    }
}
/*load user home page*/
var EA_onload_home = function(fsrc)
{
    createTab("<img src="+fsrc+"/favicon.ico>",fsrc);
}
/*event action what doing when mouse down menu or msg list*/
var EA_down_menuitem_ext = function(e)
{
    DownPass = true;
    alert(e);
}
var EA_over_menuitem_ext = function(e)
{
    DownPass = true;
    EA_out_menuitem(e);
    var eObj = __E(e);
    eObj.style.cssText = "color:red";
}
var EA_out_menuitem_ext = function(e)
{
    var eObj = __E(e);
    eObj.style.cssText = "";
}
/*evnet action what highlight menu item when mouse over it*/
var EA_over_menuitem = function(e)
{
    if(DownPass){
        DownPass = false;
        return;
    }
    if(__E(e).id) var eObj = __E(e);
    else var eObj = __E(e).parentNode;
    __pushClass(eObj,'pt_menuitem_highlight');
}
/*event action what doing when mouse down menu item*/
var EA_down_menuitem = function(e)
{
    var eObj = __E(e);
    if(eObj.id){
        var obj = eObj;
    }else{
        var eObj = $(__LoopFindId(eObj));
    }
    var id = eObj.id;
    if(DownPass)
    {
        //alert('pass the msg');
        DownPass = false;
    }else{
        switch(GH)
        {
            case 'msgBox_Btn':
                createTalkBox(id);
                setFocusBox(id);
            break;
            case "showImg":
            break;
            case "contactBtn":
                var box = createTalkBox(id, true);
                setFocusBox(id);
                if(box){
                var list = box.lastChild.firstChild.childNodes;
                EDITER = list[1];
                }
            break;
        }
    }
}
/*event action what remove highlight of menu item when mouse out item*/
var EA_out_menuitem = function(e)
{
    if(__E(e).id) var eObj = __E(e);
    else var eObj = __E(e).parentNode;
    __removeClass(eObj,'pt_menuitem_highlight');
}
/*event action what adding active frame to markbook of online*/
var EA_down_addMark = function(e)
{
    var url = $(FNP+PTFF).src;
    alert(url);
}
/*event action what create new tab*/
var EA_down_addTab = function(e)
{
    createTab("新建标签页");
}
/*event action what add http:// prefix when mouse down address input */
var EA_down_addressBarInput = function(e)
{
    var input = __E(e);
    if(input.value == '') input.value = "http://";
}
/*event action what open url what is user input when user press enter*/
var EA_enter_addressBarInput = function(e)
{
    return EA_down_openPage()
}
/*event action what open url what is user input when user down open page button*/
var EA_down_openPage = function()
{
    var url = $('addressBarInput').value;
    if(url == ''){
        alert('没有输入地址');
        return;
    }
    var id = FNP + PTFF;
    $(id).src = url;
    var tab = $('_tag'+id);
    var tabImg = tab.firstChild;
    var fav  = $(id).src+"favicon.ico";
    if(tabImg.src){
        tabImg.src = './images/load.gif';
    }else{
        tab.removeChild(tabImg);
        var tabImg = document.createElement('img');
        tabImg.src = './images/load.gif';
        tab.appendChild(tabImg);
        tab.appendChild(tab.firstChild);
    }
    var loadimg = createLoadImg(fav); 
    loadimg.onload = function(){tabImg.src= fav};
    var abort = function(){tabImg.src = '/images/nofavico.png';}
    setTimeout(abort,10000);
}
var EA_down_show_frame = function(e)
{
    if(DownPass) return DownPass = false;
    var eObj = __E(e);
    if(eObj.id)
        var tab = eObj.id;
    else
        var tab = __LoopFindId(eObj);
    if(frame == '_tag'+FNP+PTFF) return;
    var frame = tab.replace(/^_tag/g,'');
    $(FNP+PTFF).style.display = "none";
    $('_tag'+FNP+PTFF).removeClass('pt_tab_btn_active');
    $(frame).style.display = "block";
    $('addressBarInput').value = $(frame).src;
    $(tab).pushClass('pt_tab_btn_active');
    PTFF = tab.replace (/^_tag_pt_tab_/g,'');
}
/*event action what refresh active frame when mouse down refreshPage button*/
var EA_down_refreshPage = function(e)
{
    $(FNP+PTFF).src = $(FNP+PTFF).src;
}
/*event action what close frame when mouse down close tab img*/
var EA_down_close_tab = function(e)
{
    DownPass = true;
    if(PF.length==1){
         alert('No Page');
        return;
    }
    var eId = __E(e).id;
    var id = eId.replace(/_close_tag/,'');
    var n = eId.replace(/_close_tag_pt_tab_/,'');
    var index = PF.valueOf(n);
    var onblur = findNext(index);//find next active frame index
    PF.splice(index,1);
    $('activeDiv').removeChild($(id));
    $('tabBox').removeChild($('_tag'+FNP+n));
    if(n == PTFF){
        $(FNP+onblur).style.display = "block";
        $('addressBarInput').value =  $(FNP+onblur).src;
        $('_tag'+FNP+onblur).pushClass('pt_tab_btn_active');
        PTFF = onblur;
    }
}
/*event action what min talk box when mouse down min img*/
var EA_down_min_msg_box = function(e)
{
    var box = __E(e).parentNode;
    box.style.display = "none";
    
}
var EA_down_status_talk_box_btn = function(e)
{
    var eObj = __E(e);
    if(eObj.id) var btnId = eObj.id;
    else btnId = __LoopFindId(eObj);
    boxId = btnId.replace(/_status_btn$/g,'')+"_talk_box";
    var boxStyle = $(boxId).style;
    if(boxStyle.display == "none") boxStyle.display = "block";
    else boxStyle.display = "none";
}
/*close talk box when mouse down close img*/
var EA_down_close_msg_box = function(e)
{
    TALKBOXNUM -=1;
    var msgBox = __E(e).parentNode;
    var btnId = msgBox.id.replace(/_talk_box$/g,'')+"_status_btn";
    $('ptalk').removeChild(msgBox);
    $('statusBar').removeChild($(btnId));
}
var EA_over_status_talk_box_btn = function(e)
{
    
}
/*highlight min img button when mouse over min img*/
var EA_over_min_msg_box = function(e)
{
    chgTitleBtn(e);
}
/*remove highlight min button when mouse out min img*/
var EA_out_min_msg_box = function(e)
{
    chgTitleBtn(e,'r');
}
/*highlight close img button when mouse over close img*/
var EA_over_close_msg_box = function(e)
{
    chgTitleBtn(e);
}
/*remove highlight close img button when mouse out close img*/
var EA_out_close_msg_box = function(e)
{
    chgTitleBtn(e,'r');
}
/*create reply msg editer when mouse down reply button*/
var EA_down_reply_msg = function(e)
{
    DownPass = true;
    var eObj = findBtnNode(__E(e));
    var box = eObj.parentNode;
    var box_parent = box.parentNode;
    var me_info = {"type":"div","text":getDate()+" Me:","style":["pt_msg_me_send"]}
    var cancel_btn = {"type":"pt_button","text":"取消","style":["pt_btn","pt_msg_box_btn"],'event_act':"msg_reply_cancel"}
    var done_btn = {"type":"pt_button","text":"确定","style":["pt_btn","pt_msg_box_btn"],"event_act":"msg_reply_done"}
    var msgBoxReplyObj = {"type":"div","style":["pt_user_msg_input"],'sub':[me_info]}
    var msgBox = cElement(msgBoxReplyObj,box_parent);
    var pt_editer = {"type":"div","style":["pt_editer"],'event_act':'editer_focus'};
    var pt_editer_obj = cElement(pt_editer,msgBox);
    EDITER = pt_editer_obj;
    pt_editer_obj.innerHTML = "<div><p><br /></p></div>";
    var editer_div = pt_editer_obj.firstChild.firstChild;
    pt_editer_obj.focus();
    pt_editer_obj.editableDocmarker = true;
    pt_editer_obj.contentEditable = true;
    eObj.parentNode.removeChild(eObj);
    //eObj.style.display = "none";
    //msgBtn = eObj;
    cElement(done_btn,msgBox);
    cElement(cancel_btn,msgBox);
    box_parent.scrollTop = box_parent.scrollHeight - box_parent.offsetHeight;

}
/*this is test function*/
var EA_scroll_msg_box_view = function(e)
{
    var eObj = __E(e).parentNode;
    eObj.style.width = eObj.style.width + 10+"px";
}
/*do something when mouse down done button*/
var EA_down_msg_reply_done = function(e)
{
    var dBtn = findBtnNode(__E(e));
    var box = dBtn.parentNode;
    var to = box.parentNode.id.replace(/_talk_box/,'');
    var msg = box.getElementsByTagName('div')[1];
    var sendMsg = msg.innerHTML;
    if(sendMsg == '<div><p><br></p></div>')
    {
        alert('Send to '+to+' msg is empty');
        return;

    }else{
        var postmsg = {'msg':sendMsg,'to':to};
    //    A.postCall('msg/send',sendReplyStatus,postmsg);
    }
    msg.contentEditable = false;
    msg.editabledocmarker = false;
    var userMsg = msg.firstChild;
    if(msg.className == 'pt_editer')
    {
        box.removeChild(msg);
    }
    var lBtn = box.lastChild;
    box.appendChild(userMsg);
    if(lBtn === dBtn) {
        box.removeChild(lBtn);
    }else{
        box.removeChild(lBtn);
        box.removeChild(dBtn);
    }
    reBtn = cElement({"type":"pt_button","text":"回复","style":["pt_btn","pt_msg_box_btn"],"event_act":"reply_msg"},box);
    reBtn.focus(); 
}
/*cancel reply editer*/
var EA_down_msg_reply_cancel = function(e)
{
    var eObj = findBtnNode(__E(e));
    var box = eObj.parentNode;
    var msgBox = box.parentNode;
    msgBox.removeChild(box);
    reBtn = cElement({"type":"pt_button","text":"回复","style":["pt_btn","pt_msg_box_btn"],"event_act":"reply_msg"},msgBox.lastChild);
    reBtn.focus();
    //msgBtn.style.display = "block"; 
}
/*this is test*/
var EA_out_reply_msg = function(e)
{
    var eObj = __E(e);
  //  eObj.removeChild($('msg_box_reply'));
}
/*get talk pos when mouse down talk box title*/
var EA_down_move_talk_box = function(e)
{
    var box = __E(e).parentNode;
    setFocusBox(box.id);
    var boxPos = getObjPos(box);
    MGH = {'obj':box,'pos':boxPos,'down':true,'mouse':getMousePos(e)}
    DEFAULTPOS = false;
}
/*cancel move talk box status*/
var EA_up_move_talk_box = function(e)
{
   MGH.down=false;
}
/*cancel move talk box status*/
var EA_out_move_talk_box = function(e)
{
//    MGH = null;
}
/*move talk box when mosue down and move talk box title obj*/
var EA_move_move_talk_box = function(e)
{
}
var EA_down_add_b_html = function(e)
{
    var act = getActive();
    var boxid = __E(e).parentNode.parentNode.id;
    setFocusBox(boxid);
    if(act.className != 'pt_editer')
    {
        return;
    }

    var text = ptSelection();
    if(text == '')
    {
        return;
    }
    text = '<b>'+text+'</b>';
    ptExecCom('Bold'); 
}
var EA_cancel_update = function(e)
{
    var eObj = __E(e);
    var btn = findBtnNode(eObj);
    btn.parentNode.removeChild(btn);
}
var EA_down_add_att = function(e)
{
    return;
    var obj = __E(e).parentNode.nextSibling;
    alert(obj.className);
    createUpFile(obj[1]);  
}
var EA_down_add_i_html = function(e)
{
    var boxid = __E(e).parentNode.parentNode.id;
    setFocusBox(boxid);

    var act = getActive();
    if(act.className != 'pt_editer')
    {
        return;
    }
    var text = ptSelection();
    if(text == '')
    {
        return;
    }
    ptExecCom("Italic");
}
var EA_down_add_u_html = function(e)
{  
    var boxid = __E(e).parentNode.parentNode.id;
    setFocusBox(boxid);

    var act = getActive();
    if(act.className != 'pt_editer')
    {
        return;
    }
    var text = ptSelection();
    if(text == '') return;
    ptExecCom("underline");
}
var EA_down_add_link_html = function(e)
{
    var boxid = __E(e).parentNode.parentNode.id;
    setFocusBox(boxid);

    EAOutDown(e);
    var obj = __E(e);
    select = ptSelection();
    var act = getActive();
    if(act.className != 'pt_editer')
    {
        return;
    }
    if(select == '')
    {
        alert('please select some text');
        return;
        EDITER.pos = select.anchorOffset;
        createAddLinkBox(obj,false);
        return;
    }
    var link = prompt("Enter Link Address","");
    if(link)
    {
        ptExecCom("CreateLink", false, link);
    }
    //createAddLinkBox(obj,text)
}
var EA_down_char_size = function(e)
{
    var boxid = __E(e).parentNode.parentNode.id;
    setFocusBox(boxid);
}
var EA_change_char_size = function(e)
{
    var text = ptSelection();
    if(text == '') return;
    var act = text.anchorNode;
    var select = __E(e);
    var size = select.value;
    if(size == 0)
    {
        return;
    }
    ptExecCom('fontSize',false,size);
}
var showColorBox = function(e,func)
{
    var op = opDisplay('pt_color_box',func);
    if(op == true)
    {
        var color = createColorBox(__E(e).parentNode);
        EA(func,true);
    }
    if(op == "none") return;
    var colorbox = $('pt_color_box');
    var pos = getObjPos(__E(e));
    var left = pos.x;
    var tops = pos.y + pos.h; 
    colorbox.style.cssText = "left:"+left+"px;top:"+tops+"px";
}
var EA_down_show_bg_color_box = function(e)
{
    showColorBox(e,'show_bg_color_box');
    ADDCOLORMOD = 'BG';
}
var EA_down_show_char_color_box = function(e)
{
    showColorBox(e,'show_char_color_box');
    ADDCOLORMOD = 'CHAR';
}
var EA_down_get_color_value = function(e)
{
    var color = __E(e).title;
    var text = ptSelection();
    if(text == ''){
        alert('Please Select some text');
        return;
    }
    switch(ADDCOLORMOD)
    {
        case 'BG':
        var com = "hiliteColor";
        break;
        case 'CHAR':
        var com = "foreColor";
        break;
        default:
        return;
    }
    ptExecCom(com,false,color);
}
var EA_down_focus_box = function(e)
{
    var eId = __LoopFindId(__E(e));
    setFocusBox(eId);
}
var EA_down_contactBtn = function(e)
{
    var eObj = __E(e);
    if(GH != 'contactBtn') EAOutDown(e);
    if(Emt.isId('contactList') == false)
    {
        createMenu(eObj,'contactList',contactList,'menuitem','资料',true);
        EA('contactBtn',true); 
    }else{
        if($('contactList').style.display == 'none')
        {
            $('contactList').style.display = 'block';
            EA('contactBtn',true);
        }else{
            $('contactList').style.display = "none";
            EA(null,false);
        }
    }
}
var EA_down_link_cancel = function(e)
{
    DownPass = true;
    var eObj = findBtnNode(__E(e));
    var link_box = eObj.parentNode;
    link_box.parentNode.removeChild(link_box);
}
var EA_down_link_input = function(e)
{
    var eObj = __E(e);
    if(eObj.value == 'Enter URL')
    {
        eObj.value = '';
        eObj.style.cssText = 'color:#333333;';
        eObj.focus();
    }
}
var EA_focus_link_input = EA_down_link_input;
var EA_down_link_enter = function(e)
{
    var text = $('pt_add_link_text').value;
    var link = $('pt_add_link_address').value;
    if(text == '' || text == 'Enter Link Text')
    {
        alert('Please Enter Link Text');
        return;
    }
    if(link == '' || link == 'Enter URL')
    {
        alert('Please Enter Link URL');
        return;
    }
    GH.parentNode.removeChild(GH);
    var anchor = EDITER.range;
    if(anchor.nodeType == 3)
    {
        var html = anchor.nodeValue;
    }else{
        var html = anchor.innerHTML;
    }
    var tmp1 = html.substr(0,EDITER.pos);
    var tmp2 = html.substr(EDITER.pos,-1);
    var insert = '<a href="'+ link+'">'+text+'</a>';
    anchor.innerHTML = tmp1 + insert + tmp2; 
//    ptInsert('a',{"text":text,"href":link});
    EA(null,false);
}
/*
var EA_out_link_input = function(e)
{
    var eObj = __E(e);
    USER_LINK_ADD = eObj.value;
}
var EA_out_link_text = function(e)
{
    var eObj = __E(e);
    USER_LINK_TEXT = eObj.value;
}*/
var EA_down_link_text = function(e)
{
    var eObj = __E(e);
    if(eObj.value == "Enter Link Text")
    {
        eObj.value = '';
        eObj.style.cssText = 'color:#333333;';
        eObj.focus();
    }
}
var EA_focus_link_text = EA_down_link_text;
var EA_down_editer_focus = function(e)
{
//    var boxid = __E(e).parentNode.parentNode.parentNode.id;
  //  setFocusBox(boxid);

    NOWCUSERPOS = __E(e);
    EDITER.range = ptRange(); 
    //ptAtCaret();
}
